package com.gemme.gemme.webview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.view.Topview;
import com.jaeger.library.StatusBarUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;

//侧滑删除地址
public class TestZhuabaoActivity extends BaseActivity{
    private JsoupBean jsoupBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuabao);
        initTop();

        new Thread(new Runnable() {
            @Override
            public void run() {
                jsoupData();
            }
        }).start();
    }

    private void jsoupData() {
        jsoupBean = new JsoupBean();
        //抓取的目标网址
        String url = "http://www.jcodecraeer.com";

        try {//捕捉异常

            Document document = Jsoup.connect(url).get();//这里可用get也可以post方式，具体区别请自行了解

            //=======================logo    这个是泡在网上的日子的logo================
            //jsoupBean.setLogoImg();将数据放到bean的集合中list，其他也雷同，下面不做解释了
            //document.select("a.logo-t>img").attr("src")查找a标签class="logo-t"下的子标签img的属性src的值
            //document.select("a.logo-t").text()查找a标签class="logo-t"包含的内容

            jsoupBean.setLogoImg(url + document.select("a.logo-t>img").attr("src"));//select的api的详细用法请查看官方文档，这里也做简单说明
            jsoupBean.setLogoUrl(url);
            jsoupBean.setLogoName(document.select("a.logo-t").text());


            //======================导航栏1================

            Elements nv1_elements_list = document.select("ul.nav-ul>li");//查找class="nav-ul"的ul下的所有li，这里得到的是一个Elements数据
            List<String> Nv1_NameList = new ArrayList<>();
            List<String> Nv1_UrlList = new ArrayList<>();

            for (Element element : nv1_elements_list) {//遍历数组
                //                Log.e("wwww",element.select("a").attr("href"));
                //                Log.e("wwww",element.select("a").text());

                Nv1_NameList.add(element.select("a").text());//查找element下的a标签的内容
                if (element.select("a").attr("href").equals("/")) {//查找element下的a标签的href属性值
                    Nv1_UrlList.add(url);//由于 泡在网上的日子返回的是“/”，这里判断了一下再添加到Nv1_UrlList中
                } else {
                    Nv1_UrlList.add(url + element.select("a").attr("href"));
                }
            }
            jsoupBean.setNv1_NameList(Nv1_NameList);
            jsoupBean.setNv1_UrlList(Nv1_UrlList);
            //============导航栏1-1===================
            //            Log.e("wwww",document.select("a.lg_app").attr("href"));
            //            Log.e("wwww",document.select("a.lg_app").text());

            List<String> Nv1_1_NameList = new ArrayList<>();
            List<String> Nv1_1_UrlList = new ArrayList<>();

            Nv1_1_NameList.add(0, url + document.select("a.lg_app").attr("href"));//a.lg_app  ：标签.class的值      attr("href")    ：属性href的值
            Nv1_1_UrlList.add(0, document.select("a.lg_app").text());//text()    ：标签内的值

            //            Log.e("wwww",document.select("div.search_cont>form").attr("action"));
            //            Log.e("wwww",document.select("input.in_search").attr("value"));

            Nv1_1_NameList.add(1, document.select("div.search_cont>form").attr("action"));//>form  ：直接子标签form
            Nv1_1_UrlList.add(1, document.select("input.in_search").attr("value"));

            //first()   get(1)    ：由于查出来的是一个Elements（数组），所以这两个表示数组的0 1下标对应的值
            //            Log.e("wwww",document.select("div#login_info>a").get(1).toString());

            Elements nv_1_1_elements = document.select("div#login_info>a");//标签#id
            for (Element element : nv_1_1_elements) {//循环遍历数组nv_1_1_elements
                //                Log.e("wwww",element.attr("href"));
                //                Log.e("wwww",element.text());

                Nv1_1_NameList.add(url + element.attr("href"));
                Nv1_1_UrlList.add(element.text());
            }
            jsoupBean.setNv1_1_NameList(Nv1_1_NameList);
            jsoupBean.setNv1_1_UrlList(Nv1_1_UrlList);

            //============导航栏2===========================
            Elements nv_2_elements = document.select("ul#nav>li");
            List<String> Nv2_NameList = new ArrayList<>();
            List<String> Nv2_UrlList = new ArrayList<>();

            for (Element element : nv_2_elements) {//循环遍历数组nv_2_elements
                //                Log.e("wwww",element.select("a").attr("href"));
                //                Log.e("wwww",element.select("a").text());

                Nv2_NameList.add(element.select("a").text());
                Nv2_UrlList.add(url + element.select("a").attr("href"));
            }
            jsoupBean.setNv2_NameList(Nv2_NameList);
            jsoupBean.setNv2_UrlList(Nv2_UrlList);
            //=================advertigical广告     抓取不到数据，先不理，明天有空问一下公司的网页前段大神为什么，我猜应该是js注入========================
            Elements advert_elements = document.select("div.col-md-6");
            for (Element element : advert_elements) {//循环遍历数组advert_elements

                //                Log.e("wwww",element.select("a").attr("href"));
                //                Log.e("wwww",element.select("a").toString());
                //                Log.e("wwww",element.select("a>img[src$=.png]").toString());

            }
            //===============banner============================

            Elements banner_elements = document.select("div.item");

            List<String> banner_ContentList = new ArrayList<>();
            List<String> banner_UrlList = new ArrayList<>();
            List<String> banner_ImgList = new ArrayList<>();

            for (Element element : banner_elements) {//循环遍历数组banner_elements
                //                Log.e("wwww", element.select("a").attr("href"));
                //                Log.e("wwww", element.select("h3").text());
                //                Log.e("wwww", element.select("a>img").attr("src"));

                banner_ContentList.add(element.select("h3").text());
                banner_UrlList.add(element.select("a").attr("href"));
                banner_ImgList.add(url + element.select("a>img").attr("src"));
            }
            jsoupBean.setBanner_ContentList(banner_ContentList);
            jsoupBean.setBanner_UrlList(banner_UrlList);
            jsoupBean.setBanner_ImgList(banner_ImgList);
            //====================content_1========================
            Elements content1_elements = document.select("ul.arclist>li");
            List<String> content1_UrlList = new ArrayList<>();
            List<String> content1_ContentList = new ArrayList<>();

            for (Element element : content1_elements) {//循环遍历数组
                //                Log.e("wwww", element.select("a").attr("href"));
                //                Log.e("wwww", element.select("a").text());

                content1_UrlList.add(element.select("a").attr("href"));
                content1_ContentList.add(element.select("a").text());
            }
            jsoupBean.setContent1_ContentList(content1_ContentList);
            jsoupBean.setContent1_UrlList(content1_UrlList);
            //================Rank  排行榜=======================
            Elements rank_elements = document.select("ul.nav>li");
            List<String> rank_UrlList = new ArrayList<>();
            List<String> rank_ContentList = new ArrayList<>();

            for (Element element : rank_elements) {//循环遍历数组
                Log.e("wwww", element.select("a").attr("href"));
                Log.e("wwww", element.select("a").text());

                rank_UrlList.add(element.select("a").attr("href"));
                rank_ContentList.add(element.select("a").text());
            }
            jsoupBean.setRank_ContentList(rank_ContentList);
            jsoupBean.setRank_UrlList(rank_UrlList);
            System.out.println("");

        } catch (Exception e) {
            Log.e("wwwwwwwww==", e.toString());
        }
    }

    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("抓包");
    }
}